﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using FileMonitor.Configuration.Tool.Controls.Prototype;

namespace FileMonitor.Configuration.Tool.Controls
{
    public partial class WorkProcessEditor : UserControl
    {
        public WorkProcessEditor()
        {
            InitializeComponent();
            this.txt_BizProcesses.LostFocus += new EventHandler(txt_BizProcesses_LostFocus);
        }

        void txt_BizProcesses_LostFocus(object sender, EventArgs e)
        {
            string text = this.txt_BizProcesses.Text;
            bool flag = false;
            try
            {
                int i = 0;
                List<string> procedureProcesses = Program.currentForm.CurrentProcedureProcessList.Keys.ToList();
                List<string> csharpProcesses = Program.currentForm.CurrentCsharpProcessList.Keys.ToList();

                NameValueCollection values = FileMonitor.Configuration.Utility.ParseNameValueCollection(text);
                List<Int32> listIndex = new List<int>();
                foreach (string key in values.Keys)
                {
                    if (Int32.TryParse(key, out i))
                    {
                        if (!listIndex.Contains(i))
                        {
                            if (values[key].Split(",".ToCharArray()).Length == 1)
                            {
                                if (procedureProcesses.Contains(values[key]) || csharpProcesses.Contains(values[key]))
                                {
                                    flag = true;
                                }
                                else
                                {
                                    flag = false;
                                }
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                flag = false;
            }

            finally
            {
                if (!flag)
                {
                    this.lblBizProcessesError.Visible = true;
                    IsCanbeSaved = false;
                }
                else
                {
                    this.lblBizProcessesError.Visible = false;
                    IsCanbeSaved = true;
                }
            }
        }
 
        private Mode _mode;

        public Mode Mode
        {
            get { return this._mode; }
            set
            {
                if (value == Mode.Edit)
                {
                    this.txt_ProcessName.ReadOnly = true;
                    this.btnDelete.Visible = true;
                }
                this._mode = value;
            }
        }

        public delegate void SaveHandler(object sender, ProccessArgs args);
        public delegate void DeleteHandler(object sender, ProccessArgs args);

        public event SaveHandler Save;
        public event DeleteHandler Delete;
        private bool IsCanbeSaved = false;

        public void BindData(string ProcessName, NameValueCollection NormalConfig, NameValueCollection ExtendableConfig)
        {
            this.txt_ProcessName.Text = ProcessName;
            foreach (string key in NormalConfig.Keys)
            {
                string id = "txt_" + key;
                if (!this.tabNormal.Controls.ContainsKey(id))
                {
                    id = "box_" + key;
                }
                Control[] crtlArr = this.tabNormal.Controls.Find(id, false);
                if (crtlArr.Length == 1)
                {
                    crtlArr[0].Text = NormalConfig[key];
                }
            }

            foreach (string key in ExtendableConfig.Keys)
            {
                string id = "txt_" + key;
                if (!this.tabExtendabel.Controls.ContainsKey(id))
                {
                    id = "box_" + key;
                }
                Control[] crtlArr = this.tabExtendabel.Controls.Find(id, false);
                if (crtlArr.Length == 1)
                {
                    crtlArr[0].Text = NormalConfig[key];
                }
            }
            if (!string.IsNullOrEmpty(this.box_SplitArr.Text))
            {
                this.txt_SplitChar.Visible = false;
                this.chkSplitArr.Checked = true;
            }

        }

        public void BindTypeList(List<string> list)
        {
            this.box_typeName.DataSource = list;
        }

        public void BindDatabaseList(List<string> list)
        {
            this.box_TargetDatabaseName.DataSource = list;
        }

        public void BindSplitArrList(List<string> list)
        {
            this.box_SplitArr.DataSource = list;
        }

        private void OnSave(object sender, ProccessArgs args)
        {
            if (Save != null)
            {
                if (IsCanbeSaved)
                {
                    Save(this, args);
                }
                else
                {
                    MessageBox.Show("There are some error,can't be saved!!");
                }
            }
        }

        private void OnDelete(object sender, ProccessArgs args)
        {
            if (Delete != null)
            {
                Delete(this, args);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string ProcessName = this.txt_ProcessName.Text;
            NameValueCollection normalConfig = GetNormalConfig();
            NameValueCollection extConfig = GetExtConfig();
            OnSave(this, new ProccessArgs(ProcessName, normalConfig, extConfig));
        }

        private void chkSplitArr_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSplitArr.Checked)
            {
                this.txt_SplitChar.Text = "";
                this.txt_SplitChar.Visible = false;
                this.box_SplitArr.Visible = true;
            }
            else
            {
                this.txt_SplitChar.Visible = true;
                this.box_SplitArr.Visible = false;
                this.box_SplitArr.Text = "";
            }
        }

        private NameValueCollection GetNormalConfig()
        {
            NameValueCollection values = new NameValueCollection();
            foreach (Control crtl in this.tabNormal.Controls)
            {

                if (crtl is TextBox || crtl is ComboBox)
                {
                    string key = crtl.Name.Substring(crtl.Name.IndexOf("_") + 1, crtl.Name.Length - crtl.Name.IndexOf("_") - 1);
                    string value = crtl.Text;
                    values.Add(key, value);
                }
            }
            return values;
        }

        private NameValueCollection GetExtConfig()
        {
            NameValueCollection values = new NameValueCollection();
            foreach (Control crtl in this.tabExtendabel.Controls)
            {

                if (crtl is TextBox || crtl is ComboBox)
                {
                    string key = crtl.Name.Substring(crtl.Name.IndexOf("_") + 1, crtl.Name.Length - crtl.Name.IndexOf("_") - 1);
                    string value = crtl.Text;
                    values.Add(key, value);
                }
            }
            return values;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ProccessArgs args = new ProccessArgs(this.txt_ProcessName.Text, this.GetNormalConfig(), this.GetExtConfig());
            OnDelete(this, args);
        }
    }
}
